{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "681e955a",
   "metadata": {},
   "source": [
    "# Advanced operations of quantum circuit\n",
    "\n",
    "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_notebook.png)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/zh_cn/mindspore_advanced_operations_of_quantum_circuit.ipynb)&emsp;\n",
    "[![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_download_code.png)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/zh_cn/mindspore_advanced_operations_of_quantum_circuit.py)&emsp;\n",
    "[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/master/docs/mindquantum/docs/source_zh_cn/advanced_operations_of_quantum_circuit.ipynb)\n",
    "\n",
    "在前面变分量子线路操作指导中介绍了 <a href=\"https://mindspore.cn/mindquantum/docs/zh-CN/master/parameterized_quantum_circuit.html#%E9%87%8F%E5%AD%90%E7%BA%BF%E8%B7%AF\" target=\"_blank\">量子线路</a> 的基本用法。接下来，我们将进一步探索MindQuantum为量子线路定义的一些高阶操作的用法。\n",
    "\n",
    "## mindquantum.core.circuit.controlled\n",
    "\n",
    "controlled方法用于对任意量子线路或量子算符添加控制量子比特（可以是多个）。\n",
    "\n",
    "例如，我们构建一个仅包含两个量子比特的量子线路，并通过controlled方法对其添加一个控制量子比特——$q_2$：\n",
    "The basic usage of <a href=\"https://mindspore.cn/mindquantum/docs/zh-CN/master/parameterized_quantum_circuit.html#%E9%87%8F%E5%AD%90%E7%BA%BF%E8%B7%AF\" target=\"_blank\">quantum circuits</a> was introduced in the previous instruction on the operation of variable quantum circuits. Next, we will further explore the usage of some higher-order operations defined by MindQuantum for quantum circuits.\n",
    "\n",
    "## mindquantum.core.circuit.controlled\n",
    "\n",
    "The controlled method is used to add control qubits (which can be multiple) to any quantum circuit or quantum operator.\n",
    "\n",
    "For example, let's build a quantum circuit containing only two qubits and add a control qubit - $q_2$ to it by a controlled method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c44755ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"140\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"336.8\" height=\"140\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nπ/2\n </text>\n\n\n<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n\n<rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" />\n<rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f687757f340>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.algorithm.library import qft\n",
    "from mindquantum.core.circuit import controlled\n",
    "\n",
    "u1 = qft(range(2))  # Build the quantum circuit\n",
    "u1.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9330f35e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"188.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"336.8\" height=\"188.0\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"316.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<circle cx=\"92.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" />\n<circle cx=\"152.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nπ/2\n </text>\n\n<circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" />\n<line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n<circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"272.8\" x2=\"272.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" />\n<rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f687757fcd0>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u2 = controlled(u1)(2)  # Add control qubit q2 to the circuit, returning a new circuit\n",
    "u2.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d63b2ef",
   "metadata": {},
   "source": [
    "Actually, controlled() returns a function. E.g:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6144080f",
   "metadata": {},
   "outputs": [],
   "source": [
    "u3 = controlled(u1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c2a5acf",
   "metadata": {},
   "source": [
    "We can add control qubits to the initial circuit u1 by calling u3, which has the same effect as above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "39a3af2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"188.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"336.8\" height=\"188.0\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"316.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<circle cx=\"92.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" />\n<circle cx=\"152.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nπ/2\n </text>\n\n<circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" />\n<line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n<circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"272.8\" x2=\"272.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" />\n<rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f687750f400>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u4 = u3(2)\n",
    "u4.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5381a350",
   "metadata": {},
   "source": [
    "Note: The added control qubit cannot be the target qubit of a gate that already exists in the circuit, otherwise an error will be reported!\n",
    "\n",
    "In addition, we can batch add control qubits to the quantum circuit. For example, in the following example we add control qubits - $q_2$ and $q_3$ to $q_0$ and $q_1$ respectively:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bb5e74ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"248.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"336.8\" height=\"248.0\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq3:\n </text>\n<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"316.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"316.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<circle cx=\"92.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" />\n<circle cx=\"92.8\" cy=\"220.0\" r=\"4\" fill=\"#5e7ce0\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"220.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" />\n<circle cx=\"152.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" />\n<circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"220.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nπ/2\n </text>\n\n<circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#5e7ce0\" />\n<circle cx=\"212.8\" cy=\"220.0\" r=\"4\" fill=\"#5e7ce0\" />\n<line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"220.0\" stroke=\"#5e7ce0\" stroke-width=\"3\" />\n<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"212.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n<circle cx=\"272.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n<circle cx=\"272.8\" cy=\"220.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"272.8\" x2=\"272.8\" y1=\"40.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" />\n<rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f68155a2a60>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u = controlled(qft)\n",
    "u = u([2, 3], [0, 1])  # Bulk add control qubits\n",
    "u.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f991b869",
   "metadata": {},
   "source": [
    "## mindquantum.core.circuit.dagger\n",
    "\n",
    "The dagger method is used to find the Hermitian conjugates of quantum circuits or quantum operators.\n",
    "\n",
    "The following examples provide two ways of doing dagger operations:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7d203008",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"516.8\" height=\"200\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"516.8\" height=\"200\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<line x1=\"48.8\" x2=\"496.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"496.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"496.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nπ/2\n </text>\n\n<circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"212.8\" x2=\"212.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"192.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"212.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"212.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nπ/4\n </text>\n\n\n<rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"332.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"332.8\" x2=\"332.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"312.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"332.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"332.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nπ/2\n </text>\n\n\n<rect x=\"372.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"392.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<line x1=\"452.8\" x2=\"452.8\" y1=\"20.0\" y2=\"180.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n\n<rect x=\"432.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 443.2 36.31384387633061 L 448.0 28.0 L 452.8 36.31384387633061 L 449.44 36.31384387633061 L 449.44 52.0 L 446.56 52.0 L 446.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 462.40000000000003 43.68615612366939 L 457.6 52.0 L 452.8 43.68615612366939 L 456.16 43.68615612366939 L 456.16 28.0 L 459.04 28.0 L 459.04 43.68615612366939 Z\" fill=\"#ffffff\" />\n<rect x=\"432.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 443.2 156.31384387633062 L 448.0 148.0 L 452.8 156.31384387633062 L 449.44 156.31384387633062 L 449.44 172.0 L 446.56 172.0 L 446.56 156.31384387633062 Z\" fill=\"#ffffff\" />\n<path d=\"M 462.40000000000003 163.68615612366938 L 457.6 172.0 L 452.8 163.68615612366938 L 456.16 163.68615612366938 L 456.16 148.0 L 459.04 148.0 L 459.04 163.68615612366938 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f68155a2a90>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.circuit import dagger\n",
    "\n",
    "u1 = qft(range(3))\n",
    "u1.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1229c41f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"516.8\" height=\"200\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"516.8\" height=\"200\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<line x1=\"48.8\" x2=\"496.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"496.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"496.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"20.0\" y2=\"180.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 83.19999999999999 36.31384387633061 L 88.0 28.0 L 92.8 36.31384387633061 L 89.44 36.31384387633061 L 89.44 52.0 L 86.56 52.0 L 86.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 102.4 43.68615612366939 L 97.6 52.0 L 92.8 43.68615612366939 L 96.16 43.68615612366939 L 96.16 28.0 L 99.03999999999999 28.0 L 99.03999999999999 43.68615612366939 Z\" fill=\"#ffffff\" />\n<rect x=\"72.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 83.19999999999999 156.31384387633062 L 88.0 148.0 L 92.8 156.31384387633062 L 89.44 156.31384387633062 L 89.44 172.0 L 86.56 172.0 L 86.56 156.31384387633062 Z\" fill=\"#ffffff\" />\n<path d=\"M 102.4 163.68615612366938 L 97.6 172.0 L 92.8 163.68615612366938 L 96.16 163.68615612366938 L 96.16 148.0 L 99.03999999999999 148.0 L 99.03999999999999 163.68615612366938 Z\" fill=\"#ffffff\" />\n\n<rect x=\"132.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"212.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"212.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n-π/2\n </text>\n\n\n<rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"332.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"332.8\" x2=\"332.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"312.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"332.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"332.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n-π/4\n </text>\n\n<circle cx=\"392.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"372.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"392.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"392.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n-π/2\n </text>\n\n\n<rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f6815582880>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u2 = dagger(u1)\n",
    "u2.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "26239556",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"516.8\" height=\"200\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"516.8\" height=\"200\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<line x1=\"48.8\" x2=\"496.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"496.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"496.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"20.0\" y2=\"180.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 83.19999999999999 36.31384387633061 L 88.0 28.0 L 92.8 36.31384387633061 L 89.44 36.31384387633061 L 89.44 52.0 L 86.56 52.0 L 86.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 102.4 43.68615612366939 L 97.6 52.0 L 92.8 43.68615612366939 L 96.16 43.68615612366939 L 96.16 28.0 L 99.03999999999999 28.0 L 99.03999999999999 43.68615612366939 Z\" fill=\"#ffffff\" />\n<rect x=\"72.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 83.19999999999999 156.31384387633062 L 88.0 148.0 L 92.8 156.31384387633062 L 89.44 156.31384387633062 L 89.44 172.0 L 86.56 172.0 L 86.56 156.31384387633062 Z\" fill=\"#ffffff\" />\n<path d=\"M 102.4 163.68615612366938 L 97.6 172.0 L 92.8 163.68615612366938 L 96.16 163.68615612366938 L 96.16 148.0 L 99.03999999999999 148.0 L 99.03999999999999 163.68615612366938 Z\" fill=\"#ffffff\" />\n\n<rect x=\"132.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"212.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"212.8\" x2=\"212.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"192.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"212.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"212.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n-π/2\n </text>\n\n\n<rect x=\"252.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"272.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"332.8\" cy=\"160.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"332.8\" x2=\"332.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"312.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"332.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"332.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n-π/4\n </text>\n\n<circle cx=\"392.8\" cy=\"100.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"392.8\" x2=\"392.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"372.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"392.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"392.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n-π/2\n </text>\n\n\n<rect x=\"432.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"452.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f68155b15b0>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u3 = dagger(qft)\n",
    "u4 = u3(range(3))\n",
    "u4.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69117023",
   "metadata": {},
   "source": [
    "It can be seen that the circuits obtained by the two methods are the same.\n",
    "\n",
    "## mindquantum.core.circuit.apply\n",
    "\n",
    "The apply method is used to apply a quantum circuit or quantum operator to the specified qubits (which can be multiple)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "865da650",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"140\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"336.8\" height=\"140\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"152.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nπ/2\n </text>\n\n\n<rect x=\"192.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"212.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n\n<rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" />\n<rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f681552f2e0>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.circuit import apply\n",
    "\n",
    "u1 = qft([0, 1])\n",
    "circuit1 = apply(u1, [1, 0])  # Apply quantum circuit u1 to qubits q1, q0\n",
    "circuit1.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "67ed697a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"336.8\" height=\"140\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"336.8\" height=\"140\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"316.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"316.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<circle cx=\"152.8\" cy=\"40.0\" r=\"4\" fill=\"#fac209\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#fac209\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPS\n </text>\n<text x=\"152.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nπ/2\n </text>\n\n\n<rect x=\"192.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"212.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n<line x1=\"272.8\" x2=\"272.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n\n<rect x=\"252.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 36.31384387633061 L 268.0 28.0 L 272.8 36.31384387633061 L 269.44 36.31384387633061 L 269.44 52.0 L 266.56 52.0 L 266.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 43.68615612366939 L 277.6 52.0 L 272.8 43.68615612366939 L 276.16 43.68615612366939 L 276.16 28.0 L 279.04 28.0 L 279.04 43.68615612366939 Z\" fill=\"#ffffff\" />\n<rect x=\"252.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 263.2 96.31384387633061 L 268.0 88.0 L 272.8 96.31384387633061 L 269.44 96.31384387633061 L 269.44 112.0 L 266.56 112.0 L 266.56 96.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 282.4 103.68615612366939 L 277.6 112.0 L 272.8 103.68615612366939 L 276.16 103.68615612366939 L 276.16 88.0 L 279.04 88.0 L 279.04 103.68615612366939 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f681552f6d0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u2 = apply(qft, [1, 0])  # Apply qft to qubits q0, q1\n",
    "circuit2 = u2([0, 1])\n",
    "circuit2.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "553beec8",
   "metadata": {},
   "source": [
    "## mindquantum.core.circuit.add_prefix\n",
    "\n",
    "The add_prefix method is used to prefix the parameter names of parameterized quantum circuits or parameterized quantum operators. This is useful in neural network layering, for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5c70b2f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"196.8\" height=\"80\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"196.8\" height=\"80\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<line x1=\"48.8\" x2=\"176.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRX\n </text>\n<text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\ntheta\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f681559e0a0>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.circuit import add_prefix\n",
    "from mindquantum import RX, H, Circuit\n",
    "\n",
    "circ = Circuit().rx(\"theta\", 0)\n",
    "circ.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3ecfc597",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"196.8\" height=\"80\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"196.8\" height=\"80\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<line x1=\"48.8\" x2=\"176.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"112.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRX\n </text>\n<text x=\"112.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nl0_theta\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f681559e280>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ = add_prefix(\n",
    "    circ, 'l0')  # After adding, the parameter \"theta\" becomes \"l0_theta\"\n",
    "circ.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3c09fbc",
   "metadata": {},
   "source": [
    "We can prefix the parameter name of a parameterized qubit after it acts on a specific qubit, or we can add it when it has not yet been applied to the qubit. E.g:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "71f56c75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"256.8\" height=\"80\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"256.8\" height=\"80\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<line x1=\"48.8\" x2=\"236.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"132.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"172.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRX\n </text>\n<text x=\"172.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nansatz_a\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f681553a1f0>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u = lambda qubit: Circuit([H.on(0), RX('a').on(qubit)])\n",
    "\n",
    "u1 = u(0)\n",
    "u1 = add_prefix(u1, 'ansatz')\n",
    "u1.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "edbf7b03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"256.8\" height=\"80\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"256.8\" height=\"80\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<line x1=\"48.8\" x2=\"236.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"132.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"172.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRX\n </text>\n<text x=\"172.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nansatz_a\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f6815551c10>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "u2 = add_prefix(u, 'ansatz')\n",
    "u2 = u2(0)\n",
    "u2.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c05a8195",
   "metadata": {},
   "source": [
    "## mindquantum.core.circuit.change_param_name\n",
    "\n",
    "The change_param_name method is used to modify the parameter name of a parameterized quantum circuit or a parameterized quantum operator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d844ba22",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"80\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"216.8\" height=\"80\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRX\n </text>\n<text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nb\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f681554ed30>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.circuit import change_param_name\n",
    "from mindquantum import RX, H, Circuit\n",
    "\n",
    "u = lambda qubit: Circuit([H.on(0), RX('a').on(qubit)])\n",
    "\n",
    "u1 = u(0)\n",
    "u1 = change_param_name(u1, {'a': 'b'})\n",
    "u1.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3a1d5d5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"80\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"216.8\" height=\"80\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRX\n </text>\n<text x=\"152.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nb\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f68154ed3d0>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u2 = change_param_name(u, {'a': 'b'})\n",
    "u2 = u2(0)\n",
    "u2.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "522ec9fc",
   "metadata": {},
   "source": [
    "## mindquantum.core.circuit.UN\n",
    "\n",
    "The UN module is used to actuate quantum gates on different target qubits and control qubits.\n",
    "\n",
    "The general format is as follows: mindquantum.circuit.UN(gate, maps_obj, maps_ctrl=None), the parameter gate is the gate to be executed, maps_obj is the target qubit to be executed, and maps_Ctrl is the control qubit (None if there are no control qubits). If each qubit implements the same quantum gate without parameters, UN(gate, N) can be used directly, where N represents the number of qubits.\n",
    "\n",
    "Here is a simple example where we apply an H-gate to each qubit in the circuit we build:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6ba3b3ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"156.8\" height=\"260\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"156.8\" height=\"260\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq3:\n </text>\n<line x1=\"48.8\" x2=\"136.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"136.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"136.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"136.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"72.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f68155d4d00>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.circuit import change_param_name\n",
    "from mindquantum import X, H, UN, SWAP, Circuit\n",
    "\n",
    "circuit1 = Circuit()\n",
    "circuit1 += UN(H, 4)  # Apply the H gate to each qubit\n",
    "circuit1.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52a24be7",
   "metadata": {},
   "source": [
    "In the following example, we add a CNOT gate to the qubits $q_2$ and $q_0$, $q_3$ and $q_1$ respectively, where $q_2$ is the control bit, $q_0$ is the target bit; $q_3$ is the control bit , $q_1$ is the target bit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3fbb0af7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"248.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"216.8\" height=\"248.0\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq3:\n </text>\n<line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"196.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"196.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"196.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<circle cx=\"92.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"72.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"78.8\" x2=\"106.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"26.0\" y2=\"54.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<circle cx=\"152.8\" cy=\"220.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"138.8\" x2=\"166.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f681554eb20>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circuit2 = UN(X, maps_obj=[0, 1], maps_ctrl=[2, 3])\n",
    "circuit2.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e44d378e",
   "metadata": {},
   "source": [
    "针对类似SWAP的门的用法如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2ccd1cf8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"260\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"216.8\" height=\"260\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq3:\n </text>\n<line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"196.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"196.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"196.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"20.0\" y2=\"120.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 83.19999999999999 36.31384387633061 L 88.0 28.0 L 92.8 36.31384387633061 L 89.44 36.31384387633061 L 89.44 52.0 L 86.56 52.0 L 86.56 36.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 102.4 43.68615612366939 L 97.6 52.0 L 92.8 43.68615612366939 L 96.16 43.68615612366939 L 96.16 28.0 L 99.03999999999999 28.0 L 99.03999999999999 43.68615612366939 Z\" fill=\"#ffffff\" />\n<rect x=\"72.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 83.19999999999999 96.31384387633061 L 88.0 88.0 L 92.8 96.31384387633061 L 89.44 96.31384387633061 L 89.44 112.0 L 86.56 112.0 L 86.56 96.31384387633061 Z\" fill=\"#ffffff\" />\n<path d=\"M 102.4 103.68615612366939 L 97.6 112.0 L 92.8 103.68615612366939 L 96.16 103.68615612366939 L 96.16 88.0 L 99.03999999999999 88.0 L 99.03999999999999 103.68615612366939 Z\" fill=\"#ffffff\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"140.0\" y2=\"240.0\" stroke-width=\"3\" stroke=\"#16acff\" />\n\n<rect x=\"72.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 83.19999999999999 156.31384387633062 L 88.0 148.0 L 92.8 156.31384387633062 L 89.44 156.31384387633062 L 89.44 172.0 L 86.56 172.0 L 86.56 156.31384387633062 Z\" fill=\"#ffffff\" />\n<path d=\"M 102.4 163.68615612366938 L 97.6 172.0 L 92.8 163.68615612366938 L 96.16 163.68615612366938 L 96.16 148.0 L 99.03999999999999 148.0 L 99.03999999999999 163.68615612366938 Z\" fill=\"#ffffff\" />\n<rect x=\"72.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" fill=\"#16acff\" fill-opacity=\"1\" stroke=\"#ffffff\" stroke-width=\"0\" />\n<path d=\"M 83.19999999999999 216.31384387633062 L 88.0 208.0 L 92.8 216.31384387633062 L 89.44 216.31384387633062 L 89.44 232.0 L 86.56 232.0 L 86.56 216.31384387633062 Z\" fill=\"#ffffff\" />\n<path d=\"M 102.4 223.68615612366938 L 97.6 232.0 L 92.8 223.68615612366938 L 96.16 223.68615612366938 L 96.16 208.0 L 99.03999999999999 208.0 L 99.03999999999999 223.68615612366938 Z\" fill=\"#ffffff\" />\n<circle cx=\"152.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"132.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"138.8\" x2=\"166.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"152.8\" x2=\"152.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f687750a430>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circuit3 = UN(SWAP, maps_obj=[[0, 1], [2, 3]]).x(2, 1)\n",
    "circuit3.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b0d1910",
   "metadata": {},
   "source": [
    "## mindquantum.core.circuit.shift\n",
    "\n",
    "The shift method is used to modify the qubit range of a given circuit, and the parameter is the step size to be shifted. Note that the step size cannot be negative. Moreover, it can only move to the last qubit, and cannot cycle to $q_0$ (the first qubit)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "71d000ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"156.8\" height=\"128.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"12.0\" width=\"156.8\" height=\"128.0\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<line x1=\"48.8\" x2=\"136.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"136.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<circle cx=\"92.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"72.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"78.8\" x2=\"106.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f6815503370>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.circuit import shift\n",
    "from mindquantum import X, Circuit\n",
    "\n",
    "circ = Circuit().x(1, 0)\n",
    "circ.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "180fde8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"156.8\" height=\"128.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"12.0\" width=\"156.8\" height=\"128.0\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<line x1=\"48.8\" x2=\"136.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"136.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<circle cx=\"92.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"72.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"78.8\" x2=\"106.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"92.8\" x2=\"92.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f6815551dc0>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "circ = shift(circ, 1)\n",
    "circ.svg()  # The qubit acting on the circuit changes from q0, q1 to q1, q2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5050bd9",
   "metadata": {},
   "source": [
    "### Case - Iris Classification: Building Encoder\n",
    "\n",
    "The above examples of higher-order operations show the applicability and convenience of higher-order operations in quantum circuits. What magic will happen if we further combine them to build quantum circuits?\n",
    "\n",
    "接下来我们将以<a href=\"https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.5/classification_of_iris_by_qnn.html#%E9%80%9A%E8%BF%87%E9%87%8F%E5%AD%90%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E5%AF%B9%E9%B8%A2%E5%B0%BE%E8%8A%B1%E8%BF%9B%E8%A1%8C%E5%88%86%E7%B1%BB\" target=\"_blank\"> 通过量子神经网络对鸢尾花进行分类\n",
    " </a>的案例中<a href=\"https://mindspore.cn/mindquantum/docs/zh-CN/master/classification_of_iris_by_qnn.html#%E6%90%AD%E5%BB%BAencoder\" target=\"_blank\"> 搭建Encoder </a>部分为例，搭建下图所示的量子线路：\n",
    "\n",
    "Next we will start with <a href=\"https://www.mindspore.cn/mindquantum/docs/zh-CN/r0.5/classification_of_iris_by_qnn.html#%E9%80%9A%E8%BF%87%E9%87%8F%E5%AD%90%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E5%AF%B9%E9%B8%A2%E5%B0%BE%E8%8A%B1%E8%BF%9B%E8%A1%8C%E5%88%86%E7%B1%BB\" target=\"_blank\"> Classification of irises by quantum neural network\n",
    " </a><a href=\"https://mindspore.cn/mindquantum/docs/zh-CN/master/classification_of_iris_by_qnn.html#%E6%90%AD%E5%BB%BAencoder\" target=\"_blank\"> Build the Encoder part as an example, build the quantum circuit shown in the figure below:\n",
    "\n",
    "![encoder classification of iris by qnn](https://gitee.com/mindspore/docs/raw/r1.6/docs/mindquantum/docs/source_zh_cn/images/encoder_classification_of_iris_by_qnn.png)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "89bbaf78",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=====================================Circuit Summary=====================================\n",
      "|Total number of gates  : 17.                                                           |\n",
      "|Parameter gates        : 7.                                                            |\n",
      "|with 7 parameters are  : 0_alpha, 1_alpha, 2_alpha, 3_alpha, 4_alpha, 5_alpha, 6_alpha.|\n",
      "|Number qubit of circuit: 4                                                             |\n",
      "=========================================================================================\n"
     ]
    }
   ],
   "source": [
    "from mindquantum.core.circuit import shift, add_prefix\n",
    "from mindquantum import RZ, X, H, UN, Circuit\n",
    "\n",
    "template = Circuit([X.on(1, 0), RZ('alpha').on(1), X.on(1, 0)])\n",
    "encoder = UN(H, 4) + (RZ(f'{i}_alpha').on(i) for i in range(4)) + sum(add_prefix(shift(template, i), f'{i+4}') for i in range(3))\n",
    "encoder.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1686faa1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"916.8\" height=\"260\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"916.8\" height=\"260\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<text x=\"20.0\" y=\"100.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq1:\n </text>\n<text x=\"20.0\" y=\"160.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq2:\n </text>\n<text x=\"20.0\" y=\"220.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq3:\n </text>\n<line x1=\"48.8\" x2=\"896.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"896.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"896.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"48.8\" x2=\"896.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"72.8\" y=\"80.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"100.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"72.8\" y=\"140.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"160.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"72.8\" y=\"200.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"220.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"132.8\" y=\"20.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"172.8\" y=\"36.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRZ\n </text>\n<text x=\"172.8\" y=\"52.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n0_alpha\n </text>\n\n\n<rect x=\"132.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"172.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRZ\n </text>\n<text x=\"172.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n1_alpha\n </text>\n\n\n<rect x=\"132.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"172.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRZ\n </text>\n<text x=\"172.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n2_alpha\n </text>\n\n\n<rect x=\"132.8\" y=\"200.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"172.8\" y=\"216.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRZ\n </text>\n<text x=\"172.8\" y=\"232.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n3_alpha\n </text>\n\n<circle cx=\"252.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"252.8\" x2=\"252.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"232.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"238.8\" x2=\"266.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"252.8\" x2=\"252.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n\n<rect x=\"292.8\" y=\"80.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"332.8\" y=\"96.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRZ\n </text>\n<text x=\"332.8\" y=\"112.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n4_alpha\n </text>\n\n<circle cx=\"412.8\" cy=\"40.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"412.8\" x2=\"412.8\" y1=\"40.0\" y2=\"100.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"392.8\" y=\"80.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"398.8\" x2=\"426.8\" y1=\"100.0\" y2=\"100.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"412.8\" x2=\"412.8\" y1=\"86.0\" y2=\"114.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<circle cx=\"472.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"472.8\" x2=\"472.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"452.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"458.8\" x2=\"486.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"472.8\" x2=\"472.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n\n<rect x=\"512.8\" y=\"140.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"552.8\" y=\"156.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRZ\n </text>\n<text x=\"552.8\" y=\"172.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n5_alpha\n </text>\n\n<circle cx=\"632.8\" cy=\"100.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"632.8\" x2=\"632.8\" y1=\"100.0\" y2=\"160.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"612.8\" y=\"140.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"618.8\" x2=\"646.8\" y1=\"160.0\" y2=\"160.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"632.8\" x2=\"632.8\" y1=\"146.0\" y2=\"174.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<circle cx=\"692.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"692.8\" x2=\"692.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"672.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"678.8\" x2=\"706.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"692.8\" x2=\"692.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n\n<rect x=\"732.8\" y=\"200.0\" width=\"80.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#fac209\" fill-opacity=\"1\" />\n<text x=\"772.8\" y=\"216.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nRZ\n </text>\n<text x=\"772.8\" y=\"232.0\" font-size=\"14.0px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\n6_alpha\n </text>\n\n<circle cx=\"852.8\" cy=\"160.0\" r=\"4\" fill=\"#16acff\" />\n<line x1=\"852.8\" x2=\"852.8\" y1=\"160.0\" y2=\"220.0\" stroke=\"#16acff\" stroke-width=\"3\" />\n<rect x=\"832.8\" y=\"200.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#16acff\" fill-opacity=\"1\" />\n<line x1=\"838.8\" x2=\"866.8\" y1=\"220.0\" y2=\"220.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n<line x1=\"852.8\" x2=\"852.8\" y1=\"206.0\" y2=\"234.0\" stroke=\"#ffffff\" stroke-width=\"4\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x7f68154ef8e0>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "encoder.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8dcb676b",
   "metadata": {},
   "source": [
    "In the above code, we use the UN module to apply the parameter-free H gate to the target qubits $q_0$~$q_3$, and then apply the RZ(i_alpha) gate to the i-th qubit.\n",
    "Next, it is observed that there are three identical modules in the circuit, and they are all composed of a controlled X gate, a RZ gate and a controlled X gate. Therefore, we construct the corresponding template template, which is controlled by $q_0$, an X gate acting on $q_1$, an RZ gate acting on $q_1$, and an X gate controlled by $q_0$ acting on $q_1$ are formed. Then we use shift(template, i) to change the qubit range that the template acts on, and build three modules with the same structure but the qubit range differs by 1. The qubit ranges involved are $q_0$ and $q_1$, $q_1$ and $q_2$, $q_2$ and $q_3$.\n",
    "Finally, we use the add_prefix method to prefix the parameter names of all parameter quantum gates constructed by shift with a number.\n",
    "\n",
    "So far, through these advanced operations of quantum circuit provided by MindQuantum, we have built the required Encoder with only two lines of code!\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
